<?php
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use app\blog\model\StorecatModel as store;
use think\Db;

class StoreController extends AdminBaseController
{
    //销售明细报表
    //单品报表
    public function index(){
        //权限条件
        $uid=cmf_get_current_admin_id();
        $uinfo=get_operator_data($uid);
        $where=array();
        $list=array();
        $page='';
        $userList=get_user_list($uid);
        $uid_list_str=toStr($userList,'userid');
        $uid_list_str.=','.$uid;
        $store_id_list=get_store_list($uid_list_str);
        if($store_id_list){
            $where['id']=array('in',$store_id_list);
            $list = Db::name('Store')->where($where)->order('ctime desc')->paginate(10);
            $page = $list->render();
        }
        $this->assign("page", $page);
        $this->assign("list", $list);
        $this->assign('status',$uinfo['role_type']);
        return $this->fetch();
    }
    public function add(){
        //获取督导列表
        $uid=cmf_get_current_admin_id();
        $uinfo=get_operator_data($uid);
        $dudao_list=array();
        $dudao=get_user_list($uid);
        $uinfo_cz['userid']=$uid;
        $dudao[]=$uinfo_cz;
        $brandList= Db::name('Brand')->where(array('status'=>1))->select()->toarray();
        $cat_list = Db::name('StoreType')->select()->toarray();
        $goods_list = Db::name('Goods')->where(array('g_status'=>1))->select()->toarray();
        $this->assign("role_type", $uinfo['role_type']);
        $this->assign("goods_list", $goods_list);
        $this->assign("cat_list", $cat_list);
        $this->assign("brandList", $brandList);
        $this->assign("dudao_list",$dudao);
        return $this->fetch();
    }
    public function getGoods(){
        $brandid  = $_GET['brandid'];
        $goodsList=Db::name('GoodsTable')->where(array('brandid'=>$brandid,'g_status'=>1))->select()->toarray();
        return json_encode($goodsList);
    }
    /*
     * 更新在添加店铺的把商品也更新上
     * */
    public function addPost(){
        $store=new store();
        $res  = $this->request->param();
        $storeInfo=Db::name('StoreType')->where(array('id'=>$res['cat_store']))->find();
        $uid=cmf_get_current_admin_id();
        //判断手机号格式是否正确
        $res['franchise_fee']=$storeInfo['franchise_fee'];
        $res['operator']=$uid;
        $res['ctime']=time();
        $res['status']=-1;
        if(empty($res['steer'])){
            $this->error('请选择管理员','Store/add');exit;
        }
        if(empty($res['brandid'])){
            $this->error('请选择品牌','Store/add');exit;
        }
        if(empty($res['id_str'])){
            $this->error('请选择商品','Store/add');exit;
        }
        $goods_id=$res['id_str'];
        // $where['g_id']=array('in',$goods_id);
        $goods_id_list='';
        if($goods_id){
            foreach ($goods_id as $key=>$val){//编辑选择的商品id不在店铺原有商品中的id组合
                $goods_id_list.=','.$val;
                $where['g_id']=array('in',substr($goods_id_list,1));
            }
        }
        $goods=DB::name('goods_table')->where($where)->select()->toArray();
        unset($res['id_str']);
        $re=Db::name('Store')->insertGetId($res);//返回更新的店铺id
        $res=$store->goods($goods,$re);
        if($re){
            $this->success('添加成功','Store/index');
        }else{
            $this->error('添加失败','Store/add');
        }
    }
    public function edit(){
        $uid=cmf_get_current_admin_id();
        $uinfo=get_operator_data($uid);
        $id  = $this->request->param('id');
        $info=Db::name('Store')->where(array('id'=>$id))->find();//督导列表
        $dudao=get_user_list($uid);
        $uinfo_cz['userid']=$uid;
        $dudao[]=$uinfo_cz;
        $cat_list = Db::name('StoreType')->select()->toarray();
        //$goods_list = Db::name('Goods')->where(array('g_status'=>1))->select()->toarray();
        $brandList= Db::name('Brand')->where(array('status'=>1))->select()->toarray();
        $brandListGoods= Db::name('goods_table')->where(array('brandid'=>$info['brandid']))->select()->toarray();
        //获取该店铺的所有商品
        $goodsList=Db::name('goods')->where(array('storeid'=>$id))->select()->toarray();
        $this->assign("role_type", $uinfo['role_type']);
        $this->assign("brandListGoods", $brandListGoods);
        $this->assign("cat_list", $cat_list);
        $this->assign("dudao_list", $dudao);
        $this->assign("brandList", $brandList);
        $this->assign("info", $info);
        $this->assign("goodsList", $goodsList);
        return $this->fetch();

    }
    public function editPost(){
        $res  = $this->request->param();
        $storeInfo=Db::name('StoreType')->where(array('id'=>$res['cat_store']))->find();
        $uid=cmf_get_current_admin_id();
        $old=Db::name('Store')->where(array('id'=>$res['id']))->find();       
        /* if(!preg_match("/^1[34578]\d{9}$/",$res['mobile'])){
            $this->error('手机号格式不正确');
            exit;
        } */
        /* if(empty($res['address'])){
            $this->error('店铺地址必须填写');
            exit;
        } */
        $res['franchise_fee']=$storeInfo['franchise_fee'];
        $res['operator']=$uid;
        $res['jtime']=time();
        $res['status']=-1;
        $goods_id=isset($res['id_str'])?$res['id_str']:'';
        unset($res['id_str']);
        //删除原来goods表里的数据
        $store=new store();
        $goodsList=DB::name('goods')->where(array('storeid'=>$res['id'],'brandid'=>$res['brandid']))->field('g_tableid')->select()->toArray(); //店铺原有商品
        $list=array();
        foreach($goodsList as $k=>$v){
            $list[]=$v['g_tableid'];//店铺原有商品 组合一维数组
        }
        $goods_id_list='';
        $where=array();
        $goods=array();
        if($goods_id){
            foreach ($goods_id as $key=>$val){//编辑选择的商品id不在店铺原有商品中的id组合
                if(!in_array($val,$list)){
                    $goods_id_list.=','.$val;
                    $where['g_id']=array('in',substr($goods_id_list,1));
                    $goods=DB::name('goods_table')->where($where)->select()->toArray();
                }
            }
        }
        $r=$store->goods($goods,$res['id']);
        $re=Db::name('Store')->where(array('id'=>$res['id']))->update($res);
        //找到该店铺的店长
        $store=Db::name('Store')->where(array('id'=>$res['id']))->find();
        $dianzhang=Db::name('User')->where(array('id'=>$store['ownerid']))->find();
        if($dianzhang){
            if($old['steer'] != $res['steer']){
                //删除原来的关系
                deleteuser($store['ownerid'],$old['steer']);
                //添加现有关系
                adduser($store['ownerid'],$res['steer']);
            }    
        }
        if($re){
            $this->success('编辑成功','Store/index');
        }else{
            $this->error('编辑失败');
        }
    }
    function delete(){
        $id = $this->request->param("id");
        //判断这个类别下有没有店铺
        $checkM=Db::name('Member')->where(array('store_id'=>$id))->find();
        $checkU=Db::name('User')->where(array('store_id'=>$id))->find();
        if($checkM || $checkU){
            $this->error('该店铺下面有会员或店员，不能删除');
            exit;
        }
        $info = Db::name('Store')->where(['id' => $id])->find();
        if ($info){
            $status = Db::name('Store')->delete($id);
            if (!empty($status)) {
                $this->success("删除成功！", url('Store/index'));
            } else {
                $this->error("删除失败！");
            }
        }else{
            $this->error("该店铺不存在！");
        }
    }
    /*
     * 店铺审核
    */
    public function shenhe(){
        $uid=cmf_get_current_admin_id();
        $uinfo=get_operator_data($uid);
        $where=array();
        $userList=get_user_list($uid);
        $uid_list_str=toStr($userList,'userid');
        $uid_list_str.=','.$uid;
        $store_id_list=get_store_list($uid_list_str);
        if($store_id_list){
            $where['id']=array('in',$store_id_list);
        }
        $where['status']=-1;
        $list = Db::name('Store')->where($where)->paginate(10);
        // 获取分页显示
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign("list", $list);
        $this->assign('status',$uinfo['role_type']);
        return $this->fetch();

    }
    public function refuse(){
        $id=$this->request->param('id');
        $check=Db::name('Store')->where(array('id'=>$id))->find();
        if($check){
            $re=Db::name('Store')->where(array('id'=>$id))->update(['status' => -2]);
            if($re){
                $this->success('已拒绝');
            }else{
                $this->error('拒绝失败');
            }
        }else{
            $this->error('此店铺不存在');
        }
    }

    public function agree(){
        $id=$this->request->param('id');
        $check=Db::name('Store')->where(array('id'=>$id))->find();
        if($check){
            $re=Db::name('Store')->where(array('id'=>$id))->update(['jtime' =>time(), 'status' => 1]);
            if($re){
                $this->success('审核通过');
            }else{
                $this->error('审核失败');
            }
        }else{
            $this->error('此店铺不存在');
        }
    }
}
